/**
 * ...
 * @author Rob Dravenstott
 */
import com.chumby.WidgetParams;
import com.chumby.util.Delegate;
class Main 
{
	private static var _widgetParams:WidgetParams = null;
	private static var _appRoot:MovieClip;
	private static var _wc:WeatherRadarConfig = null;
	
	public static function main(swfRoot:MovieClip):Void 
	{
		_appRoot = swfRoot;
		var app = new Main(swfRoot);
	}
	
	public function Main() 
	{
		_quality = "high";

		// Set the scale mode locked so we can resize to stage properly
		Stage.scaleMode = "noScale";
		//Stage.align = "TL";
				
		// if we're running on a chumby
		// establish listener functions and retrieves parameters
//		if (_level0._chumby_user_id != undefined)
//		{
			_widgetParams = new WidgetParams();
			_widgetParams.addEventListener(WidgetParams.GOT_PARAMS,Delegate.create(_root, gotParameters));
			_widgetParams.addEventListener(WidgetParams.SENT_PARAMS,Delegate.create(_root, sentParameters));
			_widgetParams.getWidgetParams();
//		}
		// Not running on a chumby so just continue with default params
/*		else
		{
			var event:Object = new Object();
			event.status=false;
			gotParameters(event);
		}*/
	}
	private function gotParameters(event:Object) 
	{
		var settings = new Object();
		
		if (!event.status) 
		{
			trace("Could not load parameters, using defaults.");
		} 	
		else
		{
			//Read the chumby parameters
			var parameters:Object = _widgetParams.chumbyParams();
			
			if (parameters["radar_zoom"] != undefined)
				settings.radar_zoom = parameters["radar_zoom"];
			if (parameters["radar_type"] != undefined)
				settings.radar_type = parameters["radar_type"];
			if (parameters["radar_max_images"] != undefined)
				settings.radar_max_images = parameters["radar_max_images"];
			if (parameters["radar_id"] != undefined)
				settings.radar_id = parameters["radar_id"];
		}

		// Check to make sure we didn't pass in an override via html flashvars appurl param
		if (_level0.radar_zoom != undefined)
			settings.radar_zoom = _level0.radar_zoom;
		if (_level0.radar_type != undefined)
			settings.radar_type = _level0.radar_type;
		if (_level0.radar_max_images != undefined)
			settings.radar_max_images = _level0.radar_max_images;
		if (_level0.radar_id != undefined)
			settings.radar_id = _level0.radar_id;

		//Run the actual presentation code
		_wc = new WeatherRadarConfig(_appRoot, settings, Main.saveNewSettings);
	}
		
	// Save the data to the server
	public static function saveNewSettings (settings:Object, saveIt:Boolean) 
	{
		if (   (saveIt == undefined)
		    || (saveIt == true))
		{
			var parameters:Object = new Object();
					
			parameters["radar_zoom"] = String(settings.radar_zoom);
			parameters["radar_type"] = String(settings.radar_type);
			parameters["radar_max_images"] = String(settings.radar_max_images);
			parameters["radar_id"] = String(settings.radar_id);

			// Only save if we're on a chumby
			if (_widgetParams != null)
			{
				_widgetParams.sendWidgetParams(parameters);
			}
			else
			{
				_wc.showTextMessage("Not Chumby",14);
			}
		}
		else
			closeBox();
	}
	
	public static function closeBox()
	{
			getURL('javascript:dismiss()');
	}

	// called as a callback when done saving the data to the server
	private function sentParameters(event:Object) 
	{
		if (!event.status) {
			_wc.showTextMessage("Error",14);
			trace("Error sending parameters.");
			return;
		}
		else
		{
			_wc.showTextMessage("Saved",14);
			closeBox();
		}
	}
		
}